package com.atguigu.realtime.app.dwm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONAware;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.realtime.app.BaseAppV1;
import com.atguigu.realtime.commont.Constant;
import com.atguigu.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternSelectFunction;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.PatternTimeoutFunction;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.SimpleCondition;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.OutputTag;

import java.time.Duration;
import java.util.List;
import java.util.Map;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/11/26 9:15
 */
public class DwmUserJumpDetailApp_1 extends BaseAppV1 {
    public static void main(String[] args) {
        new DwmUserJumpDetailApp_1().init(3002,
                                          1,
                                          "DwmUserJumpDetailApp_1",
                                          "DwmUserJumpDetailApp_1",
                                          Constant.TOPIC_DWD_PAGE);
    }
    
    @Override
    protected void run(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        
       /* stream = env.fromElements(
            "{\"common\":{\"mid\":\"101\"},\"page\":{\"page_id\":\"home\"},\"ts\":10000} ",
            "{\"common\":{\"mid\":\"101\"},\"page\":{\"page_id\":\"home\"},\"ts\":11000} ",
            "{\"common\":{\"mid\":\"102\"},\"page\":{\"page_id\":\"home\"},\"ts\":10000}",
            "{\"common\":{\"mid\":\"102\"},\"page\":{\"page_id\":\"good_list\",\"last_page_id\":" +
                "\"home\"},\"ts\":11000} ",
            "{\"common\":{\"mid\":\"102\"},\"page\":{\"page_id\":\"good_list\",\"last_page_id\":" +
                "\"detail\"},\"ts\":50000} ");*/
        
        KeyedStream<JSONObject, String> keyedStream = stream
            .map(JSON::parseObject)
            .assignTimestampsAndWatermarks(
                WatermarkStrategy
                    .<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                    .withTimestampAssigner((log, ts) -> log.getLong("ts"))
            )
            .keyBy(log -> log.getJSONObject("common").getString("mid"));
        
        // 1. 定义模式
        Pattern<JSONObject, JSONObject> pattern = Pattern
            .<JSONObject>begin("entry1")
            .where(new SimpleCondition<JSONObject>() {
                @Override
                public boolean filter(JSONObject value) throws Exception {
                    JSONObject page = value.getJSONObject("page");
                    return page.getString("last_page_id") == null || page.getString("last_page_id").length() == 0;
                }
            })
            .next("entry2")
            .where(new SimpleCondition<JSONObject>() {
                @Override
                public boolean filter(JSONObject value) throws Exception {
                    JSONObject page = value.getJSONObject("page");
                    return page.getString("last_page_id") == null || page.getString("last_page_id").length() == 0;
                }
            })
            .within(Time.seconds(5));
        
        // 2. 把模式作用到流上
        PatternStream<JSONObject> ps = CEP.pattern(keyedStream, pattern);
        // 3. 获取匹配到的数据或者超时数据
        SingleOutputStreamOperator<JSONObject> result = ps.select(
            new OutputTag<JSONObject>("entry") {},
            new PatternTimeoutFunction<JSONObject, JSONObject>() {
                @Override
                public JSONObject timeout(Map<String, List<JSONObject>> map, long timeoutTimestamp) throws Exception {
                    return map.get("entry1").get(0);
                }
            },
            new PatternSelectFunction<JSONObject, JSONObject>() {
                @Override
                public JSONObject select(Map<String, List<JSONObject>> map) throws Exception {
                    return map.get("entry1").get(0);
                    
                }
            }
        );
    
        result
            .union(result.getSideOutput(new OutputTag<JSONObject>("entry") {}))
            .map(JSONAware::toJSONString)
            .addSink(FlinkSinkUtil.getKafkaSink(Constant.TOPIC_DWM_UJ_DETAIL));
    }
}
